#include "tetlshorter.h"

QSharedPointer<Shorter> TeTlShorterCreator::create(QSharedPointer<QSettings> settings)
{
    Shorter *s = new TeTlShorter(settings);
    return QSharedPointer<Shorter>(s, &QObject::deleteLater);
}

TeTlShorter::TeTlShorter(QSharedPointer<QSettings> s, QObject *parent) :
        Shorter(s, parent)
{
    setName("Te.tl");
    setShorterSite(QUrl("http://te.tl/"));
}

void TeTlShorter::doShortLink(const QUrl &url)
{
    QUrl reqUrl("http://te.tl/api.php");

    reqUrl.addQueryItem("url",url.toString());
    reqUrl.addQueryItem("private","1");

    QNetworkReply *r = manager()->get(QNetworkRequest(reqUrl));
    applyAttribute(r, url);
}

void TeTlShorter::doFinish(QNetworkReply *reply)
{
    QString s(reply->readAll());

    if (reply->error() != QNetworkReply::NoError)
        emit done(Shorter::Error,"",s);
    else
        emit done(Shorter::Ok,getAttribute(reply),s.trimmed());
}
